class TreeNode:
    def __init__(self, name, parent_node):
        self.name = name
        self.parent_node = parent_node
        self.child_node_list = []


class Tree:
    def __init__(self):
        self.root_node = None

    def add_node(self, name, parent_name):
        if self.root_node is None:
            self.root_node = TreeNode(name, None)
        else:
            parent_node = self.get_node(parent_name)
            if parent_node is None:
                raise Exception(f"父节点:{parent_name}不存在")
            parent_node.child_node_list.append(TreeNode(name, parent_node))

    def get_node(self, name):
        if self.root_node is None:
            return None
        if self.root_node.name == name:
            return self.root_node
        return self._get_from_child_node(name, self.root_node)

    def delete_node(self, name):
        if self.root_node is None:
            raise Exception("空树无法删除节点")
        target_node = self.get_node(name)
        if target_node is None:
            return
        target_node.parent_node.child_node_list.remove(target_node)

    def _get_from_child_node(self, name, node):
        if node.name == name:
            return node
        for child_node in node.child_node_list:
            result = self._get_from_child_node(name, child_node)
            if result is not None:
                return result
        return None